package UI;

import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class UIsort extends JDialog implements ActionListener {
    JPanel p1,p2;
    JButton jButton_add;
    JButton jButton_move;
    JLabel jLabel;
    JTextField jTextField;
    String[][] obj;//表格数据
    JTable table;
    JScrollPane scrollpane; //表格面板
    String arr[];

    String sql_all = "SELECT * FROM sort";
    ResultSet resultSet;
    JdbcDeal jdbcDeal;
    int Count;        //获取信息行数

    private boolean Table_select(String sql){
        //初始化数据
        try {
            jdbcDeal = new JdbcDeal();
            resultSet = jdbcDeal.select(sql);
            ResultSetMetaData rsmd = resultSet.getMetaData();        // 返回元数据对象
            resultSet.last();
            Count = resultSet.getRow();
            resultSet.beforeFirst();
            obj = new String[Count][arr.length];
            int i=0;
            if(!resultSet.next()){
                jdbcDeal.close();
                JOptionPane.showMessageDialog(this, "未查询到相关信息", "", JOptionPane.WARNING_MESSAGE);
                return false;
            }else{
                do{
                    obj[i][0] = resultSet.getString("Sname");
                    i++;
                }while (resultSet.next());
                jdbcDeal.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return true;
    }

    private void UpdateTable(){
        p2.remove(scrollpane);
        table=new JTable(obj,arr);
        table.setRowHeight(30);
        scrollpane=new JScrollPane(table);
        p2.add(scrollpane,BorderLayout.CENTER);
        p2.revalidate();
        this.revalidate();
    }

    public UIsort() {
        this.setSize(400,350);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setModal(true);    //模态窗口
        p1 = new JPanel();
        Box boxH = Box.createHorizontalBox(); //行式盒
        jLabel = new JLabel("添加信息");
        jTextField = new JTextField(15);
        jButton_add = new JButton("添加");
        jButton_move = new JButton("删除");

        boxH.add(jLabel);
        boxH.add(jTextField);
        boxH.add(jButton_add);
        boxH.add(jButton_move);
        p1.add(boxH);

        arr = new String[]{"类别"};
        Table_select(sql_all);

        table=new JTable(obj,arr);
        //表格限宽
        table.setRowHeight(30);
        //创建显示表格的滚动面板
        scrollpane=new JScrollPane(table);

        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(scrollpane,BorderLayout.CENTER);
        Box boxV = Box.createVerticalBox();
        boxV.add(p1);
        boxV.add(p2);

        jButton_add.addActionListener(this);
        jButton_move.addActionListener(this);

        this.add(boxV,BorderLayout.CENTER);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String texts = jTextField.getText();
        if(e.getSource() == jButton_add){
            if(texts.equals("")){
                JOptionPane.showMessageDialog(this, "请输入添加信息!", "", JOptionPane.WARNING_MESSAGE);
                return;
            }
            JdbcDeal jdbcDeal = new JdbcDeal();
            try {
                if(jdbcDeal.Sort_add(texts) == 0){
                    JOptionPane.showMessageDialog(this, "添加失败(可能已存在)", "", JOptionPane.WARNING_MESSAGE);
                    jdbcDeal.close();
                }else{
                    JOptionPane.showMessageDialog(this,"添加成功","",JOptionPane.PLAIN_MESSAGE);
                    jdbcDeal.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            Table_select(sql_all);
            UpdateTable();
            return;
        }else if(e.getSource() == jButton_move){
            if(table.getSelectedRows().length>1 || table.getSelectedRow()==-1) {
                JOptionPane.showMessageDialog(this, "请选择一行！", "", JOptionPane.WARNING_MESSAGE);
                return;
            }else{
                String table_Sname = (String) table.getValueAt(table.getSelectedRow(),0);
                String sql = "DELETE FROM sort WHERE Sname = '" + table_Sname + "'";
                try {
                    jdbcDeal = new JdbcDeal();
                    if(jdbcDeal.del(sql) > 0){
                        JOptionPane.showMessageDialog(this, "成功删除一条记录！","",JOptionPane.PLAIN_MESSAGE);
                        Table_select(sql_all);
                        UpdateTable();
                        return;
                    } else {
                        JOptionPane.showMessageDialog(this, "删除记录失败！","", JOptionPane.WARNING_MESSAGE);
                        Table_select(sql_all);
                        UpdateTable();
                        return;
                    }
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(this, "删除记录失败！可能和其他信息有联动！请先删除相关信息！","", JOptionPane.WARNING_MESSAGE);
                    throwables.printStackTrace();
                }

            }

        }
    }
}
